<?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Db\Adapter\Adapter as DbAdapter;
use Application\Form\ConnexionForm;
use Application\Form\InscriptionForm;
use Application\Model\EspaceClient;
use Application\Model\Inscription;
use Application\Model\ClientTable;
use Zend\Session\Container;

class EspaceclientController extends AbstractActionController {
	
	protected $clientTable;
	
	public function espaceclientAction() {
		$client_session = new Container('client');
		
		if($client_session->is_connected==1){
			return new ViewModel();	
		
		}else {
		
			return $this->redirect ()->toRoute ( 'connexion_client', array (
					'controller' => 'Espaceclient',
					'action' => 'connexion',
			) ,
			array(
	    	 				'query' => array('url' => 'espaceclient'),
	    	 		));
		}
	}
	
	
	public function connexionAction() {
		date_default_timezone_set('UTC');
		$client_session = new Container('client');
		$espaceform = new ConnexionForm();
		$request = $this->getRequest ();
		if ($request->isPost ()) {
				
			$sample = new EspaceClient ();
			$espaceform->setInputFilter ( $sample->getInputFilter () );
			$espaceform->setData ( $request->getPost () );
				
			if ($espaceform->isValid ()) {
		
				$datas = $espaceform->getData ();
				// $datas->exchangeArray($form->getData());
		
				
				$login = $datas ['login'];
				$password = $datas ['password'];
		
				$client = $this->getClientTable()->getClientLogin($login);
		
				if($client != null){
					
					if($login == $client->login && $password == $client->password){
					
						$client_session->is_connected =1;
					
						return $this->redirect ()->toRoute ( 'espaceclient', array (
								'controller' => 'Espaceclient',
								'action' => 'espaceclient',
						) );
						
						
					}else {
					
					
						$result = 'Votre login ou mot de passe est incorrecte';
						return new ViewModel(array(
							'result' =>$result,
							'espaceform' => $espaceform,
						));
					}
					
				}else {
					$result = 'Votre login n\'existe pas. Songez à créer votre compte';
					return new ViewModel(array(
							'result' =>$result,
							'espaceform' => $espaceform,
					));
				}
		
				/*	return $this->redirect ()->toRoute ( 'formation', array (
				 'controller' => 'Formation',
						'action' => 'formation',
						'params' => 'ok'
				) );*/
			} 
		}
		
		return new ViewModel(array(
				'espaceform'=>$espaceform,
		));
		
	}
	
	
	public function getClientTable() {
		if (! $this->clientTable) {
			$sm = $this->getServiceLocator ();
			$config = $sm->get ( 'config' );
    		$config = $config['db'];
    		$dbAdapter = new DbAdapter($config);
    		//return $dbAdapter;
    		$table = new ClientTable($dbAdapter);
    		//$this->userTable = $sm->get($table);
    		$this->clientTable = $table;
    	}
    	return $this->clientTable;
    }
    
    public function inscriptionAction(){
    	date_default_timezone_set('UTC');
    	$form = new InscriptionForm();
    	$request = $this->getRequest ();
    	if ($request->isPost ()) {
    		
    		$sample = new Inscription();
    		$form->setInputFilter($sample->getInputFilter());
    		$form->setData($request->getPost());
    		 
    		if ($form->isValid()) {
    		
			echo 'is valid';
    			$datas = $form->getData() ;
    			//$datas->exchangeArray($form->getData());
    		
    			$nom  = $datas['nom'];
    			$prenom  = $datas['prenom'];
    			$fonction  = $datas['fonction'];
    			$entreprise  = $datas['entreprise'];
    			$email  = $datas['email'];
    			$telephone = $datas['telephone'];
    			$login = $datas['login'];
    			$password  = $datas['password'];
    			$password2  = $datas['password2'];
    			
    			$objet  = $datas['motif'];
    			$date_inscrip =  $datas['date_inscrip'];
    			
    		//	echo isPasswordEqual($password,$password2);
    			
    			if($password != $password2){
    	
    				$erreur = 'Erreur de confirmation du mot de passe';
    				return new ViewModel(array(
    						'form'=>$form,
    						'erreur' =>$erreur,
    				));
    			}
    			
    			$result = $this->getClientTable()->getClientLogin($login);
    			try {
    				$this->getClientTable()->addClient($nom, $prenom,$fonction, $entreprise, $email, $telephone, $login, md5($password), $objet,$date_inscrip);
    			} catch (\PDOException $e) {
    				
    				throw new Exception\InvalidQueryException(
    						'Statement could not be executed ',
    						null,
    						$e
    				);
    			}
 

    			return new ViewModel(array(
    					'result'=>'<p style="padding-left: 8px;">Votre compte a été enregistré.Vous veillez attendre sa 
    					 validation pour vous connecter <br><br> Merci <br><br>
    					<a href="#"> Cliquez ici pour revenir à la page d\'acceuil </a>
    					
    					</p>
    					
    					',
    			));
    		}
    	}
    	return new ViewModel(array(
    		'form'=>$form,
    	));
    }
}
